<?php
/**
 * Created by PhpStorm.
 * User: wuanjie
 * Date: 16-6-8
 * Time: 下午2:21
 */
class AppointmentController extends \Core_BaseCtl {

    public function indexAction() {
        $serviceModel = new ServiceModel();
        $service = $serviceModel->getLists(array('order'=>'sort'));
        
        $this->_view->assign('service', $service);
        $this->_view->assign('pageTitle', '其它服务预约');
    }

    public function makeAppointmentAction() {
        
        $appointmentModel = new AppointmentModel();
        if ($this->getRequest()->isPost()) {

            $pdata = $this->getAllPost();
            $pdata['openID'] = $this->openid;

            if (!$pdata['openID']) {
                $content = 'openID不存在!';
                $url = '/appointment';
                $this->redirect(Tools_help::url("redirect/error?c=$content&u=$url"));
            }

            //用户不能在一天提交超过10次
            $cache_config = $this->config['cache'];
            $cache_config['cache_dir'] .= '/appointment';
            $cache = Cache_Cache::create($cache_config, Cache_Cache::TYPE_FILE);
            $userip = Tools_help::getIp();
            $check = $cache->get(md5($userip));
            $history = array();
            if ($check) {
                if ($check['count'] >= 10) {
                    $content = "提交次数一天不能超过10次";
                    $url = "/order/makeComboOrder";
                    $this->redirect(Tools_help::url("redirect/error?c=$content&u=$url"));
                    exit;
                }
                $history['count'] = $check['count'] + 1;
            } else {
                $history['count'] = 1;
            }
            $check = $cache->set(md5($userip), $history, 3600 * 24);

            // 验证
            $result = $appointmentModel->validation->validate($pdata, 'default');
            if ($result) {

                $data = array();
                $data['service_id'] = $pdata['service_id'];
                $data['service_type'] = $pdata['service_type'];
                $data['client_type'] = $pdata['client_type'];
                $data['client_name'] = $pdata['client_name'];
                $data['client_phone'] = $pdata['client_phone'];
                $data['appointment_date'] = strtotime($pdata['appointment_date']);
                $data['appointment_address'] = $pdata['appointment_address'];
                $data['appointment_detail'] = $pdata['appointment_detail'];

                if ($appointmentModel->insert($data)) {
                    $content = "提交成功！稍后将会有工作人员与您联系。";
                    $url = "/appointment/index";
                    $this->redirect(Tools_help::url("redirect/index?c='$content&u=$url"));exit;
                } else {
                    $content = "提交失败！";
                    $url = "/appointment/index";
                    $this->redirect(Tools_help::url("redirect/error?c='$content&u=$url"));exit;
                }
            }
        }
    }

    public function getServiceAction() {
        $service_id = $this->getp('service_id', 0);
        if (empty($service_id)) {
            die(json_encode(0));
        }

        $model = new ServiceModel();
        $result = $model->getLists(array('where' => array('ser_id' => $service_id)));
        if ($service_id) {
            die(json_encode($result));
        }
        die(json_encode(0));
    }
}